package com.hdw.med.controller;

import com.hdw.common.base.BaseException;
import com.hdw.common.base.BaseResult;
import com.hdw.common.constant.CommonResultConstant;
import com.hdw.common.db.DynamicDataSource;
import com.hdw.common.db.RoutingDataSource;
import com.hdw.common.util.SpringContextUtil;
import com.hdw.common.vo.LayMsg;
import com.hdw.common.vo.PaginationVO;
import com.hdw.med.api.BaseDbService;
import com.hdw.med.bean.model.BaseDb;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * baseDb控制器
 * Created by hozeData on 2021/05/21.
 */
@RestController
@RequestMapping("/etl/baseDb/")
@Api(tags = "数据源")
@CrossOrigin(origins = "*", maxAge = 3600)
public class BaseDbController {


    @Autowired
    BaseDbService baseDbService;

    @ApiOperation(value = "分页查询")
    @RequestMapping(value = "findByPage", method = RequestMethod.POST)
    public BaseResult findByPage(@RequestBody BaseDb baseDb, Integer pageNum, Integer pageSize) {
        PaginationVO<BaseDb> pageVO = baseDbService.findPageByModel(baseDb, pageNum, pageSize);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }


    @ApiOperation(value = "查询")
    @RequestMapping(value = "find", method = RequestMethod.GET)
    public LayMsg find(Integer page, Integer limit) {
        List<BaseDb> pageVO = baseDbService.findByModel(new BaseDb());
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }


    @ApiOperation(value = "查询可用DB")
    @RequestMapping(value = "findEnableDB", method = RequestMethod.GET)
    public LayMsg findEnableDB() {
        BaseDb baseDb = new BaseDb();
        baseDb.setEuDbTp("1");
        baseDb.setFgStop("0");
        List<BaseDb> pageVO = baseDbService.findByModel(baseDb);
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value = "保存")
    @ApiImplicitParam(name = "baseDb", value = "实体", paramType = "body", required = true, dataType = "BaseDb")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public LayMsg save(@RequestBody BaseDb baseDb, HttpServletRequest request) {
        int num;
        BaseDb param = new BaseDb();
        param.setEuDbTp("2");
        List<BaseDb> byModel = baseDbService.findByModel(param);


        if (baseDb.getIdDb() == null) {
            if ("2".equals(baseDb.getEuDbTp())) {
                if (byModel.size() > 0) return LayMsg.fail().addMsg("使用类型：数据仓库，已存在！");
            }
            num = baseDbService.insertSelective(baseDb);
        } else {
            if ("2".equals(baseDb.getEuDbTp())) {
                if (byModel.size() > 0 && !byModel.get(0).getIdDb().equals(baseDb.getIdDb())) {
                    return LayMsg.fail().addMsg("使用类型：数据仓库，已存在！");
                }
            }
            num = baseDbService.updateByIdSelective(baseDb);
        }
        return LayMsg.success().addMsg("成功");
    }


    @ApiOperation(value = "测试")
    @ApiImplicitParam(name = "baseDb", value = "实体", paramType = "body", required = true, dataType = "BaseDb")
    @RequestMapping(value = "test", method = RequestMethod.POST)
    public BaseResult test(@RequestBody BaseDb baseDb, HttpServletRequest request) {
        String msg = baseDbService.testDB(baseDb);

        return new BaseResult(CommonResultConstant.SUCCESS, msg);
    }

    @ApiOperation(value = "详情")
    @ApiImplicitParam(name = "idDb", value = "主键", paramType = "query", required = true, dataType = "Long")
    @RequestMapping(value = "getById", method = RequestMethod.GET)
    public BaseResult getById(Long idDb, HttpServletRequest request) throws BaseException {
        BaseDb baseDb = baseDbService.getById(idDb);
        return new BaseResult(CommonResultConstant.SUCCESS, baseDb);
    }

    @ApiOperation(value = "修改")
    @ApiImplicitParam(name = "baseDb", value = "实体", paramType = "body", required = true, dataType = "BaseDb")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public BaseResult update(@RequestBody BaseDb baseDb, HttpServletRequest request) {
        if ("2".equals(baseDb.getEuDbTp())) {
            BaseDb param = new BaseDb();
            param.setEuDbTp("2");
            List<BaseDb> byModel = baseDbService.findByModel(param);
            if (byModel.size() > 0 && !byModel.get(0).getIdDb().equals(baseDb.getIdDb())) {
                return new BaseResult(CommonResultConstant.ERROR, "使用类型：数据仓库，已存在！");
            }
        }
        int num = baseDbService.updateByIdSelective(baseDb);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value = "根据主键删除")
    @ApiImplicitParam(name = "idDb", value = "主键", paramType = "query", required = true, dataType = "Long")
    @RequestMapping(value = "delete", method = RequestMethod.GET)
    public BaseResult delete(Long idDb, HttpServletRequest request) throws BaseException {
        int num = baseDbService.deleteById(idDb);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value = "动态切换数据源测试")
    @RequestMapping(value = "test", method = RequestMethod.GET)
    public Object testDynamicDataSource(Integer page, Integer limit) {
        BaseDbController bean = SpringContextUtil.getBean(BaseDbController.class);

        Map<String, Object> result = new LinkedHashMap<>();
        result.put("RoutingDataSource,无参-数据仓库", bean.test());
        result.put("RoutingDataSource,DW-数据仓库", bean.test1());
        result.put("RoutingDataSource,base_db.idDb指定库：MySQL", bean.test2());
        result.put("RoutingDataSource,base_db.idDb指定库：Oracle", bean.test3());
        result.put("RoutingDataSource,base_db.idDb指定库：SQLServer", bean.test4());
        return result;
    }


    @RoutingDataSource(IdDb = "67735350277050883")
    public List<Map<String, Object>> test4() {
        List<Map<String, Object>> result = null;
        try {
            result = baseDbService.testDynamicDataSource();
        } catch (Exception e) {
        }
        return result;
    }

    @RoutingDataSource(IdDb = "67722551559722247")
    public List<Map<String, Object>> test3() {
        List<Map<String, Object>> result = null;
        try {
            result = baseDbService.testDynamicDataSource();
        } catch (Exception e) {
        }
        return result;
    }

    @RoutingDataSource(IdDb = "6754985683032961")
    public List<Map<String, Object>> test2() {
        List<Map<String, Object>> result = null;
        try {
            result = baseDbService.testDynamicDataSource();
        } catch (Exception e) {
        }
        return result;
    }

    @RoutingDataSource(IdDb = "DW")
    public List<Map<String, Object>> test1() {
        List<Map<String, Object>> result = null;
        try {
            result = baseDbService.testDynamicDataSource();
        } catch (Exception e) {
        }
        return result;
    }

    @RoutingDataSource
    public List<Map<String, Object>> test() {
        List<Map<String, Object>> result = null;
        try {
            result = baseDbService.testDynamicDataSource();
        } catch (Exception e) {
        }
        return result;
    }

}
